package playerData;

import gui.MainFrame;
import java.awt.Image;

public class PlayerMove extends Thread {

    private int x, y;
    private boolean threadRunning, threadPause;
    private boolean up, down, left, right, direction;
    private Image activePic;
    private final Image picLeft, picRight;
    private final int imageHeight, imageWidth;
    private Player p;

    public PlayerMove(Player p) {
        this.p = p;
        picLeft = p.getPlayerLeft();
        picRight = p.getPlayerRight();
        imageHeight = picLeft.getHeight(null);
        imageWidth = picLeft.getWidth(null);
        up = false;
        down = false;
        right = false;
        left = false;
        x = p.getX();
        y = p.getY();
        activePic = picRight;
        direction = false;
        start();
    }

    @Override
    public void run() {
        threadRunning = true;
        threadPause = false;
        while (threadRunning) {
            if (threadPause) {
                continue;
            }
            try {
                movePlayer();
                PlayerMove.sleep(15);
            } catch (InterruptedException ex) {
                System.out.println("" + ex.getMessage());
            }
        }
    }

    private void movePlayer() {
        if ((up == true) && (y >= 3)) {
            y -= 3;
        } else if ((down == true) && ((y + imageHeight) < (MainFrame.pnSpielflaeche.getHeight() - 2))) {
            y += 3;
        }
        if ((left == true) && (x >= 3)) {
            x -= 3;
            activePic = picLeft;
            direction = true;
        } else if ((right == true) && ((x + imageWidth) < (MainFrame.pnSpielflaeche.getWidth() - 2))) {
            x += 3;
            activePic = picRight;
            direction = false;
        }
    }

    public boolean collide(int x, int y, int width, int height) {
        //Check player hinterseite falls Waffe höher als Spieler
        if (((x <= (this.x + this.imageWidth)) && ((x + width) >= (this.x + this.imageWidth))) && ((y <= this.y) && ((y + height) >= (this.y + this.imageHeight)))) {
            return true;
        }

        //Check player vorderseite falls Waffe höher als Spieler
        if (((x <= this.x) && ((x + width) >= (this.x))) && ((y <= this.y) && ((y + height) >= (this.y + this.imageHeight)))) {
            return true;
        }

        //Check ob bei player die Waffe an der unterseite berührt wird
        if (((y <= this.y) && ((y + height) >= this.y)) && ((x <= this.x) && ((x + width) >= (this.x + this.imageWidth)))) {
            return true;
        }

        //Check ob bei player die Waffe an der oberseite berührt wird
        if ((((y <= (this.y + this.imageHeight) && ((y + height) >= (this.y + this.imageHeight))))) && ((x <= this.x) && ((x + width) >= (this.x + this.imageWidth)))) {
            return true;
        }

        //Prüfe ob die Rakete im Player gestartet wurde
        if (((y > this.x) && (y < (this.y + this.imageHeight))) && ((x < this.x) && ((this.x + this.imageWidth) < (x + width)))) {
            return true;
        }

        //Prüfe ob die Waffe höher als Player ist
        if (((y < this.x) && ((y + height) > (this.y + this.imageHeight))) && ((this.x < x) && ((x + width) < (this.x + this.imageWidth)))) {
            return true;
        }

        //prüfe ob die linke obere Ecke  sich in Player befindet
        if ((y >= this.y) && (x >= this.x) && (y <= (this.y + this.imageHeight)) && (x <= ((this.x + this.imageWidth)))) {
            return true;
        }
        //prüfe ob die linke untere Ecke  sich in Player befindet
        if (((y + height) >= this.y) && (x >= this.x) && (x <= ((this.x + this.imageWidth))) && ((y + height) <= (this.y + this.imageHeight))) {
            return true;
        }
        //prüfe ob die rechte obere Ecke  sich in Player befindet
        if (((y >= this.y) && (y <= (this.y + this.imageHeight))) && ((x + width) >= this.x) && ((x + width) <= ((this.x + this.imageWidth)))) {
            return true;
        }
        //prüfe ob die rechte untere Ecke  sich in Player befindet/
        if (((y + height) >= this.y) && ((y + height) <= (this.y + this.imageHeight)) && ((x + width) >= this.x) && ((x + width) <= ((this.x + this.imageWidth)))) {
            return true;
        }
        
        return false;
    }

    public void setDown(boolean down) {
        this.down = down;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public Image getActivePic() {
        return this.activePic;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public int getImageHeight() {
        return this.imageHeight;
    }

    public int getImageWidth() {
        return this.imageWidth;
    }

    public boolean getDirection() {
        return this.direction;
    }

    public void pauseThread() {
        threadPause = true;
    }

    public void resumeThread() {
        threadPause = false;
    }

    public void stopThread() {
        threadRunning = false;
    }

    public void receiveDamage(int damage) {
        p.receiveDamage(damage);
    }

    public String getPlayersName() {
        return p.getName();
    }

    public boolean getDead() {
        return p.getDead();
    }
}
